<template>
	<view class="page-container">
		<view class="phone-bar">手机权限申请</view>
		<view class="phone-con">为了账号安全，请绑定手机号，如拒绝将登录失败</view>
		<button class="phone-bind-btn" type="primary" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">手机号一键绑定</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				
			}
		},
		methods: {
			
			// 获取手机号
			async getPhoneNumber(e){
				let phone, params, session_key, phoneRes;
				session_key = uni.getStorageSync('session_key');
				params = {
					method: 'api/v2/member_login/get_phone_by_mini_program',
					sessionKey: session_key,
					encryptedData: e.detail.encryptedData,
					iv: e.detail.iv
				}
				phoneRes = await this.$http(params);
				if(phoneRes.status){
					phone = phoneRes.data.phoneNumber;
					this.phoneNumber = phone;
					uni.setStorageSync('phone', phone);
					this.xcxBindLogin()
				}else{
					uni.showToast({
						title: phoneRes.msg,
						icon: "none"
					})
				}
			},
			
			// 小程序登录
			async xcxBindLogin(){
				// 获取分销邀请人员的分销码
				
				let params, nickname, litpic, mobile, openid, loginRes,fxpcode,unionid;
				nickname = uni.getStorageSync('nickname');
				litpic = uni.getStorageSync('headimg');
				mobile = uni.getStorageSync('phone');
				openid = uni.getStorageSync('openid');
				unionid = uni.getStorageSync('unionid');
				unionid = unionid ? unionid : '';
				fxpcode = uni.getStorageSync('fxpcode');
				fxpcode = fxpcode ? fxpcode : '';
				
				params = {
					method: "api/v2/member/login/login_by_phone",
					nickname,
					litpic,
					mobile,
					comefrom: "wechat",
					openid,
					fxpcode,
					unionid
				}
				loginRes = await this.$http(params);
				if(loginRes.status){
					this.$store.commit('login', loginRes.data.memberInfo)
					uni.setStorageSync('token', loginRes.data.token);
					uni.setStorageSync('userInfo', loginRes.data.memberInfo);
					uni.showToast({
						title: '绑定成功',
						icon: 'none'
					})
					setTimeout(() => {
						uni.navigateBack({
							delta: 3
						})
					}, 1000)
				}else{
					uni.showToast({
						title: loginRes.msg,
						icon: "none"
					})
				}
			}
			
		}
	}
</script>

<style lang="scss">
	.page-container{
		padding: 20rpx;
	}
	.phone-bar{
		padding: 20rpx 0;
		text-align: center;
		border-bottom: 2rpx solid $uni-border-color;
		font-size: $uni-font-size-lg;
	}
	.phone-con{
		padding: 40rpx 20rpx;
		text-align: center;
		font-size: $uni-font-size-base;
	}
	.phone-bind-btn{
		border-radius: $uni-border-radius-lg;
		background-color: $uni-bg-color-primary !important;
	}
</style>
